package com.example.demo.controller;


import com.example.demo.service.AnalyseService;
import com.example.demo.service.GoodsService;
import com.example.demo.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AnalyseController
{
    @Value("${ip}")
    private String ip;
    @Value("${server.port}")
    private String port;
    @Value("${uploadGoodsPath}")
    private String uploadGoodsPath;
    @Value("${uploadIconPath}")
    private String uploadIconPath;
    @Value("${goodsPicPathPattern}")
    private String goodsPicPathPattern;
    @Value("${iconPathPattern}")
    private String iconPathPattern;
    @Resource
    private GoodsService goodsService;
    @Resource
    private AnalyseService analyseService;
    @RequestMapping("/dashboard")
    public String Analyse(Model model)
    {
        List<Integer> analyse=new ArrayList<>();
        analyse.add(50000);
        analyse.add(20000);
        model.addAttribute("analyse",analyse);
        model.addAttribute("goods",goodsService.getAll());
        return "admin/dashboard";
    }

    @RequestMapping("/loadData")
    @ResponseBody
    public Object loadData()
    {
        Map<String, Object> map=new HashMap<String, Object>();
        List<Double> analyse=new ArrayList<>();
        List<String> times=new ArrayList<>();
        for(int i=0;i>-7;i--)
        {
            String firstDay=TimeUtils.getFirstDayOfMonth(i);
            String lastDay=TimeUtils.getLastDayOfMonth(i);
            analyse.add(analyseService.getProfitByMonth(firstDay,lastDay));
            times.add(firstDay);
        }
        map.put("analysis",analyse);
        map.put("times",times);
        return map;
    }


    @RequestMapping("/analyse/{id}")
    public String AnalyseGoods(Model model, @PathVariable("id")Integer id,
                               @RequestParam(value = "startTime",required = false)String startTime,
                               @RequestParam(value = "endTime",required = false)String endTime)
    {
        model.addAttribute("good",goodsService.getGoodsById(id));
        if (startTime!=null&&endTime!=null)
        {
            String s[]=startTime.split("T");
            startTime=s[0]+" "+s[1];
            s=endTime.split("T");
            endTime=s[0]+" "+s[1];
            System.out.println(startTime);
            System.out.println(endTime);
            model.addAttribute("startTime",startTime);
            model.addAttribute("endTime",endTime);
            model.addAttribute("analyseGood", analyseService.getAnalyseGood(id,startTime ,endTime));
        }
        return "admin/analyseGoods";
    }
}
